<?php

namespace app\index\controller;

class Test
{
    private $cookie = RUNTIME_PATH . 'cookie/';
    private $cookie_file = '';

    public function curl()
    {
        $url = "https://api2.aiyuangong.com/user/login";

        $data = [];
        $data['phone'] = '15102003725';
        $this->cookie_file = $this->cookie . $data['phone'] . date('-Ymd') . '.cookie';
        $uinfo = \think\Cache::get('userinfo_new');
        if (!is_file($this->cookie_file) || !$uinfo) {
            $data['password'] = '123456';
            $data['withInfo'] = true;
            $uinfo = $this->http_request($url, json_encode($data), null, null, true);
            \think\Cache::set('userinfo_new', $uinfo, 3600);
            dump($uinfo);
            exit;
        }

        $this->getSignIn();
        $this->doSignIn();
    }

    //获取签到列表
    private function getSignIn($cache = true)
    {
        $signInfo = \think\Cache::get('signinfo' . date('-Ymd'));
        if (!$cache || !$signInfo) {
            $uinfo = \think\Cache::get('userinfo');
            $setting = $uinfo['result']['userDetail'];
            $url = 'https://api2.aiyuangong.com/signIn/getSignInInfo';
            $data = [
                'firmId' => $setting['firmId'],
                'accountId' => ''
            ];
            $signInfo = $this->http_request($url, json_encode($data));
            $signInfo = $signInfo['result'];
            \think\Cache::set('signinfo' . date('-Ymd'), $signInfo);
        }

        return $signInfo;
    }

    //签到签退
    private function doSignIn()
    {
        $signInfo = $this->getSignIn();
        $signIn = $signInfo['signIn'];
        $position = $this->getPosition();
        if ($signIn['startWorkSignInPointId'] < 1) { //签到
            $position['signInAction'] = 1;
            $rs = $this->call_api('signIn/signIn', $position);
            dump($rs);
            $this->getSignIn(false);
            exit;
        }

        if ($signIn['endWorkSignInPointId'] < 1) { //签退
            $position['signInAction'] = 2;
            $rs = $this->call_api('signIn/signIn', $position);
            dump($rs);
            $this->getSignIn(false);
            exit;
        }
        
        dump($signInfo);
    }

    //获取签到坐标
    private function getPosition()
    {
        $signInfo = $this->getSignIn();
        $point = $signInfo['signInPoint'];
        $rand1 = rand(1, 200) / 1000000;
        $rand2 = rand(1, 200) / 1000000;
        $isReduce1 = rand(0, 1);
        $isReduce2 = rand(0, 1);
        $place = [
            '广东省广州市天河区建中路靠近中国建筑第四工程局有限公司华南分公司',
            //'广东省广州市天河区建中路59号'
        ];
        return [
            'position' => $place[array_rand($place)],
            'longitude' => $point['longitude'] + $rand1 * ($isReduce1 ? -1 : 1),
            'latitude' => $point['latitude'] + $rand2 * ($isReduce2 ? -1 : 1),
        ];
    }

    //签到提醒设置
    private function signInSetting()
    {
        $uinfo = \think\Cache::get('userinfo');
        $setting = $uinfo['result']['signInInfo']['signInSetting'];
        unset($setting['accountId']);
        $setting['startWorkRemind'] = false;
        $setting['beforeMinute'] = 0;
        return $this->call_api('signIn/setSignInRemindSetting', $setting);
    }
    
    //调用接口
    private function call_api($api, $para = [])
    {
        $url = 'https://api2.aiyuangong.com/'.$api;
        return $this->http_request($url, json_encode($para), null, null, true);
    }

    /**
     * http请求，支持GET和POST
     * 传入data对象则使用post传输
     * 传入timeout 则启用超时设置
     * 传入head 为true值 则返回响应头数据
     * 传入iscookie,则存cookie进/shop/cookie文件夹
     */
    public function http_request($url, $data = null, $timeout = null, $head = null, $iscookie = null)
    {
        //初始化curl
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

        if (!empty($iscookie)) {//是否存cookie
            !is_dir($this->cookie) && @mkdir($this->cookie, 777, TRUE);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_file); //以手机存储用户的cookie文件
        }
        if (!empty($this->cookie_file) && is_file($this->cookie_file)) {
            curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file);
        }

        //转化data成array
        if (!empty($data)) {
            if (is_string($data)) {
                $header = array(
                    'Content-Type: application/json; charset=utf-8',
                    'Content-Length: ' . strlen($data)
                );
            } else {
                //$data = http_build_query($data);
                $header = array(
                    'Content-Type: application/x-www-form-urlencoded; charset=utf-8'
                );
            }

            $header [] = 'Connection: keep-alive';
            $header [] = 'User-Agent: ozilla/5.0 (X11; Linux i686) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.186 Safari/535.1';
            $header [] = 'Accept:application/json,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
            $header [] = 'Accept-Language: zh-CN,zh;q=0.8';
            $header [] = 'Accept-Charset: utf-8;q=0.7,*;q=0.3';
            $header [] = 'Cache-Control:max-age=0';

            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }

        //运行curl，结果以jason形式返回
        $res = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        return json_decode($res, true);
    }

}
